package com.code.monitor.listener;

import com.code.monitor.config.ServerConfig;
import com.code.monitor.handler.*;
import com.code.monitor.message.HeartBeatMessage;
import com.code.monitor.model.CacheManager;
import com.code.monitor.model.MachineInfo;
import com.code.monitor.model.PackageInfo;
import com.code.monitor.model.ServiceInfo;
import com.code.monitor.resolve.MethodReslover;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.util.Date;

/**
 * @author ggh
 * @date 2023/4/15
 */
@Slf4j
@Component
public class ServerListener{
    @Autowired
    private ServerConfig serverConfig;

    @PostConstruct
    public void startServer() throws InterruptedException {
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup();

        String webSocketPath = serverConfig.getWebsocketPath();

        SerializableMessageCodec serializableMessageCodec = new SerializableMessageCodec();
        MessageHandler messageHandler = new MessageHandler();

        HttpMessageHandler httpMessageHandler = new HttpMessageHandler();
        WebSocketConnectHandler webSocketConnectHandler = new WebSocketConnectHandler();
        WebSocketMessageHandler webSocketMessageHandler = new WebSocketMessageHandler();

        MethodReslover.init();
//        mockDataTest();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        nioSocketChannel.pipeline().addLast(new ProtocolAnalyHandler(serializableMessageCodec, messageHandler,
                                httpMessageHandler, webSocketConnectHandler, webSocketMessageHandler, webSocketPath));
                    }
                }).bind(new InetSocketAddress(serverConfig.getPort())).sync();
        log.info("网络服务已启动，端口：{}", serverConfig.getPort());
    }

    private void mockData() {
        HeartBeatMessage heartBeatMessage = new HeartBeatMessage();
        heartBeatMessage.setIp("192.168.1.1(示例)");
        heartBeatMessage.setServiceName("service-consumer");
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setIp(heartBeatMessage.getIp());
        serviceInfo.setOnlineTime(new Date());
        serviceInfo.setServiceName(heartBeatMessage.getServiceName());
        SocketChannel socketChannel = new NioSocketChannel();
        CacheManager.setServiceInfo(socketChannel, serviceInfo);

        HeartBeatMessage heartBeatMessage2 = new HeartBeatMessage();
        heartBeatMessage2.setIp("192.168.1.2(示例)");
        heartBeatMessage2.setServiceName("service-producer");
        ServiceInfo serviceInfo2 = new ServiceInfo();
        serviceInfo2.setIp(heartBeatMessage2.getIp());
        serviceInfo2.setOnlineTime(new Date());
        serviceInfo2.setServiceName(heartBeatMessage2.getServiceName());
        SocketChannel socketChannel2 = new NioSocketChannel();
        CacheManager.setServiceInfo(socketChannel2, serviceInfo2);
    }

    private void mockDataTest() {
        MachineInfo machineInfo = new MachineInfo();
        machineInfo.setIp("192.168.1.121");
        machineInfo.setSshPort(122);
        machineInfo.setUsername("yjk");
        machineInfo.setPassword("yjktest");
        machineInfo.setPackagejar("1234");
        machineInfo.setSftpRoot("C:\\tmp");
        CacheManager.addMachineInfo(machineInfo);

        PackageInfo packageInfo = new PackageInfo();
        packageInfo.setName("service-demo");
        packageInfo.setFileName("service-demo-1.0-SNAPSHOT.jar");
        packageInfo.setUuid("1234");
        packageInfo.setPath("C:\\Users\\10419\\Desktop\\service\\20230518-uWNABIwO\\service-demo-1.0-SNAPSHOT.jar");
        CacheManager.addPackageInfo(packageInfo);
    }
}
